package com.team.ai.service;

import com.team.ai.entity.AiList;
import com.team.ai.entity.Content;
import com.team.ai.entity.ResultEntity;
import com.team.ai.entity.User;
import com.team.ai.mapper.AiListMapper;
import com.team.ai.mapper.ContentMapper;
import com.team.ai.mapper.UserMapper;
import com.team.ai.tool.MD5Tool;
import jakarta.annotation.Resource;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private static final Logger logger= LoggerFactory.getLogger(UserService.class);//日志getLogger需要传入的类名
    @Resource
    UserMapper userMapper;
    @Resource
    AiListMapper aiListMapper;

    @Resource
    ContentMapper contentMapper;

    /**
     * 登录
     * @param name 用户名
     * @param password 密码
     * @return
     */
    public User login(String name, String password){
        try{
            return userMapper.login(name,password);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return  null;
        }
    }

    /**
     * 用户注册
     */
    public String insert(String name,String password,int gender,String email,String phone,String nickName,String avatar){
        try{
            String ans = ResultEntity.success(userMapper.insert(name, password, gender, email, phone, nickName,avatar));
            System.out.println(ans);
            if (ans.contains("200")){
                User user = userMapper.selectByName(name);
                int userId = user.getId();
                String title = "文案";
                String text = "你好，我是你的文案生成器。我会通过你提供的文本内容，帮你生成吸引人的文案~！";
                String pic="http://s4oh1k5hp.bkt.clouddn.com/wa.png";
                aiListMapper.insert(title,text,userId,pic);
                AiList aiList = aiListMapper.selectByTitleUserid(title,userId);
                int aiId = aiList.getId();
                contentMapper.addq(null,text,userId,aiId,"");

                title = "智能问答";
                text = "你好，智能问答帮手，我能根据您的问题来回复";
                pic="http://s4oh1k5hp.bkt.clouddn.com/znwa.png";
                aiListMapper.insert(title,text,userId,pic);
                aiList = aiListMapper.selectByTitleUserid(title,userId);
                aiId = aiList.getId();
                contentMapper.addq(null,text,userId,aiId,"");


                title = "EXCEL助手";
                text = "你好，我是你的EXCEL助手，可以帮你解答任何关于Excel使用的问题。";
                pic="http://s4oh1k5hp.bkt.clouddn.com/excel.png";
                aiListMapper.insert(title,text,userId,pic);
                aiList = aiListMapper.selectByTitleUserid(title,userId);
                aiId = aiList.getId();
                contentMapper.addq(null,text,userId,aiId,"");

                title = "翻译助手";
                text = "你好，我是你的翻译助手。告诉我需要翻译的内容和目标语种，我会尽快为你翻译~";
                pic="http://s4oh1k5hp.bkt.clouddn.com/fy.png";
                aiListMapper.insert(title,text,userId,pic);
                aiList = aiListMapper.selectByTitleUserid(title,userId);
                aiId = aiList.getId();
                contentMapper.addq(null,text,userId,aiId,"");

            }
            return ans;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return  ResultEntity.fail(-10005);
        }
    }

    /**
     * 修改用户信息
     */
    public int update(String name,String phone,String email,int gender,String nickName,String remark,int id){
        try{
            return userMapper.update(name, phone, email, gender, nickName,remark, id) ;
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return  -1;
        }
    }

    /**
     *修改密码
     */
    public boolean updatepwd(int userId,String password){
        try{
            return userMapper.updatepwd(userId,password);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return false;
        }
    }

    /**
     * 上传头像
     */

    public String addAvatar(int id,String avatar){
        try{
            return ResultEntity.success(userMapper.addAvatar(id, avatar));
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return  ResultEntity.fail(-10005);
        }
    }







    /**
     * 根据id获取用户详情
     * @param id 用户id
     * @return String
     */
public User selectByPrimaryKey(int id){
    try{

        return userMapper.selectByPrimaryKey(id);
    }catch (Exception e){
        logger.error(e.getLocalizedMessage());
        return null;
    }
}

    /**
     * 根据手机号查询
     * @param phone
     * @return
     */
    public User selectByPhone(String phone){
    try{
        return userMapper.selectByPhone(phone);
    }catch (Exception e){
        logger.error(e.getLocalizedMessage());
        return null;
    }
}

    /**
     * 根据名称查询
     * @param name
     * @return
     */
    public User selectByName(String name){
        try{
            return userMapper.selectByName(name);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }



}
